const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '请选择截止时间',
    toolBool:false,
    industryBool:false,
    areaTypeShow:false,
    areaType:[
      {
        name:'不限地区',
        value:0,
      },
      {
        name:'指定地区',
        value:1,
      }
    ],
    tools:[
      {
        name:'暂不启用',
        value: 0
      },
      {
        name:'立即启用',
        value: 1
      },
    ],
    industryList:[],
    industryData:{
      minIndex:0,
      name:'',
      id:'',
    },
    mainActiveIndex:0,
    activeId: null,
    dataShow:{
      toolShow:'立即启用',
    },
    array:["暂不启用","立即启用"],
    indexs:1,
    region: [],
    upImg:[],//图片路径
    upImgId:[],//图片ID
    taskType:'',//1是招标任务2是悬赏任务
    taskTitleVal:'',
    taskDescriptionVal:'',
    contactVal:'',
    //isTeamworkOpen:'',
    areaList:[],
    columns:[],
    areaShow:false,

    surrenteList:[],
    surrenteShow:false,
    surrenteName:'',
    surrente_stage:null,
    scene:'',
    maincolor:'',
    conference:'',
    // 地区信息
    id:'',
    subid:'',
    aresname:'不限地区',
    // 任务信息
    taskid: '',
    tasktitle: '',
    tasksubtitle: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      taskType: options.type
    })
    // if(options.type == 1){
    //   this.teamworkOpen()
    // }
    let surrenteList=app.globalData.dict['CURRENT_STAGE']
    this.setData({
      surrenteList,
    });
    this.areaList()
    this.industryList()
  },

  areaTypeClose(){
    this.setData({
      areaTypeShow:false,
    })
  },


  areaTypeSelect(e){
   const {value}=e.detail
    if(value===1){
      this.setData({
        areaTypeShow:false,
        areaShow:true
      })
    }else {
      this.setData({
        areaTypeShow:false,
        aresname:'不限地区',
        id:'',
        subid:'',
      })
    }

  },

  surrentebtn(){
    this.setData({
      surrenteShow: true,
    });
  },

  surrenteCancel(){
    this.setData({
      surrenteShow: false,
    });
  },

  surrentefirm(e){
    console.log(e,'surrentefirm-----')
    const {itemText,itemValue}=e.detail.value
    this.setData({
      surrenteShow: false,
      surrenteName:itemText,
      surrente_stage:itemValue,
    });
  },


  onClickNav({ detail = {} }) {
    this.setData({
      mainActiveIndex: detail.index || 0,
    });
  },

  onClickItem({ detail = {} }) {
    this.setData({
      activeId:detail.id,
      ['industryData.name']: detail.name,

    });
  },

  industryChange(){
    const {mainActiveIndex,activeId,industryData}=this.data
    // let val=this.data.industryList[mainActiveIndex].name+''+industryData.name
    this.setData({
      fieldValue:industryData.name,
      taskid:activeId,
      ['industryData.minIndex']:mainActiveIndex,
      ['industryData.id']: activeId,
      industryBool: false,
    })
  },

  industryClose(){
    const {minIndex,name,id}=this.data.industryData
    this.setData({
      industryBool: false,
      mainActiveIndex:minIndex,
      activeId:id,
    })
  },

  industryClick(){
    this.setData({
      industryBool: true
    })
  },

  toolClick(){
    this.setData({
      toolBool: true
    })
  },

  toolClose(){
    this.setData({
      toolBool: false
    })
  },

  toolSelect(event){
      console.log(event,'event--------')
    this.setData({
      ['dataShow.toolShow']:event.detail.name,
      indexs:event.detail.value,
    })
  },

  taskDescriptionTap(){
    wx.navigateTo({
      url: '/userPage/pages/storeText/storeText?type=1&text='+this.data.taskDescriptionVal,
    })
  },

  taskScene(){
    wx.navigateTo({
      url: '/userPage/pages/storeText/storeText?type=6&text='+this.data.scene,
    })
  },

  taskColor(){
    wx.navigateTo({
      url: '/userPage/pages/storeText/storeText?type=7&text='+this.data.maincolor,
    })
  },

  taskConference(){
    wx.navigateTo({
      url: '/userPage/pages/storeText/storeText?type=8&text='+this.data.conference,
    })
  },

  industryList(){
    var _this = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'taskCate',
      data: {},
      success(res) {
        wx.hideLoading()
        let dataList=res.data.data
        if (res.data.code == 1000 && dataList.length>0) {
          dataList.forEach(item=>{
            item['text']=item.name
            item['children']=item.children_task
            item['children']?.forEach(res=>{
              res['text']=res.name
            })
          })
          _this.setData({
            industryList: dataList
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },



  // teamworkOpen(){
  //   let _this = this
  //   var url = app.globalData.baseUrl + 'myTask/isTeamworkOpen';
  //   var data = {
  //     token: app.globalData.token
  //   }
  //   wx.showLoading({
  //     title: '加载中',
  //   })
  //   app.wxGetRequest(url, data,(res) =>{
  //     wx.hideLoading()
  //     if (res.code == 1000){
  //       _this.setData({
  //         isTeamworkOpen:res.data
  //       })
  //     }
  //   }, (err) =>{
  //     wx.hideLoading()
  //     wx.showToast({
  //       title: err.data.message,
  //       icon: 'none',
  //       duration: 2000
  //     })
  //   })
  // },
  // 上传图片
  upImgBtn: function () {
    // var _this = this;
    // var { upImg, upImgId } = this.data
    // wx.chooseImage({
    //   count: 1,
    //   sizeType: ['original', 'compressed'],
    //   sourceType: ['album', 'camera'],
    //   success(res) {
    //     const tempFilePaths = res.tempFilePaths
    //     wx.showLoading({
    //       title: '加载中',
    //     })
    //     wx.uploadFile({
    //       url: app.globalData.baseUrl + 'fileUpload',
    //       filePath: tempFilePaths[0],
    //       name: 'file',
    //       formData: {
    //         token: app.globalData.token,
    //         file: tempFilePaths[0]
    //       },
    //       success(upres) {
    //         wx.hideLoading()
    //         var obj = JSON.parse(upres.data)
    //         const data = upres.data.url
    //         upImg.push(obj.data.url)
    //         upImgId.push(obj.data.id)
    //         _this.setData({
    //           upImg: upImg,
    //           upImgId: upImgId
    //         })
    //       }
    //     })
    //   }
    // })

    var _this = this;
    var { upImg, upImgId } = this.data
    wx.chooseMessageFile({
      count: 3,
      type: 'all',
      success(res) {
        const tempFiles = res.tempFiles
        wx.showLoading({
          title: '加载中',
        })
        wx.uploadFile({
          url: app.globalData.baseUrl + 'fileUpload',
          filePath: tempFiles[0].path,
          name: 'file',
          formData: {
            token: app.globalData.token,
            file: tempFiles[0].path
          },
          success(upres) {
            wx.hideLoading()
            var obj = JSON.parse(upres.data)
            const data = upres.data.url
            if (obj.code == 1000) {
              upImg.push(obj.data.url)
              upImgId.push(obj.data.id)
              _this.setData({
                upImg: upImg,
                upImgId: upImgId
              })
            } else {
              wx.showToast({
                title: obj.message,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
    })
  },
  // 删除图片
  delImg: function (e) {
    var { upImg, upImgId } = this.data;
    var { index } = e.currentTarget.dataset
    upImg.splice(index,1)
    upImgId.splice(index, 1)
    this.setData({
      upImg,
      upImgId
    })
  },
  // 标题
  taskTitleInp:function(e){
    this.setData({
      taskTitleVal: e.detail.value
    })
  },
  // 描述
  taskDescriptionInp: function (e) {
    this.setData({
      taskDescriptionVal: e.detail.value
    })
  },
  // 联系
  contactInp: function (e) {
    this.setData({
      contactVal: e.detail.value
    })
  },
  // 选择行业
  industryBtn: function () {
    wx.navigateTo({
      url: '/taskPage/pages/areaList/areaList?type=0',
    })
  },

  onChange(event) {
    const { picker, value, index } = event.detail;
    picker.setColumnValues(1, this.data.areaList[value[0]]);
  },

  areafirm(e){
    let data=e.detail.value
    let aresname
    aresname=data[0]+'-'+data[1].name
    this.setData({
      id:data[1].upid,
      subid:data[1].id,
      aresname
    })
    this.areaCancel()
  },

  areaCancel(){
    this.setData({
      areaShow:false
    })
  },


  // 选择地址
  areaBtn: function () {
    this.setData({
      areaTypeShow:true
    })
  },

  /**
   * 获取地址列表
   */
  areaList(){
    let _this = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: app.globalData.baseUrl + 'task/district',
      data: {},
      success(res) {
        wx.hideLoading()
        if (res.data.code == 1000) {
          let list=_this.areaListHand(res.data.data)
          let listKey=Object.keys(list)
          let columns=[
            {
              values: listKey,
              className: 'column1',
            },
            {
              values: list[listKey[0]],
              className: 'column2',
              defaultIndex: 2,
            },
          ]
          _this.setData({
            areaList: list,
            columns,
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  areaListHand(data) {
    let list = {}
    data.forEach(item=>{
      list[item.name]=item.child
    })
    return list
  },

  /*// 选择地址
  areaBtn: function () {
    wx.navigateTo({
      url: '/taskPage/pages/areaList/areaList?type=1',
    })
  },*/
  deliveryTool(e){
    this.setData({
      indexs: e.detail.value
    })
  },
  // 下一步
  nextBtn:function(){
    var {
      subid,
      taskid,
      taskTitleVal,
      taskDescriptionVal,
      contactVal,
      surrente_stage,
      scene,
      scene,
      maincolor,
      conference,
    } = this.data
    if (taskTitleVal == ''){
      wx.showToast({
        title: '请输入标题',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (taskDescriptionVal == '') {
      wx.showToast({
        title: '请输入描述',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (taskid == '') {
      wx.showToast({
        title: '请选择行业',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (contactVal == '') {
      wx.showToast({
        title: '请输入联系方式',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (!(/^1[3-9]\d{9}$/.test(contactVal))) {
      wx.showToast({
        title: '手机号格式错误',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (!conference) {
      wx.showToast({
        title: '请输入竞品参考',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (!maincolor) {
      wx.showToast({
        title: '请输入主题色',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (!scene) {
      wx.showToast({
        title: '请输入使用场景',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (!surrente_stage) {
      wx.showToast({
        title: '请选择需求阶段',
        icon: 'none',
        duration: 2000
      })
      return false
    }
   /* if (subid == '') {
      wx.showToast({
        title: '请选择地区',
        icon: 'none',
        duration: 2000
      })
      return false
    }*/
    wx.navigateTo({
      url: '/taskPage/pages/releaseTaskNext/releaseTaskNext?taskType='+this.data.taskType,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
